<?php
class curl_vbulletin
{
	/*
		@ Variable	: $curl (Resource)
		@ About		: The cURL object used for the request
		@ Type		: Private
	*/
	var $curl = null;

	/*
		@ Variable	: $cookie_name (String)
		@ About		: The filename of the temp file used for storing cookies
		@ Type		: Private
	*/
	var $cookie_name = array();

	/*
		@ Variable	: $vbulletin_url (String)
		@ About		: The address of the remote vbulletin that is being connected to
		@ Type		: Private
	*/
	var $vbulletin_url = null;

	/*
		@ Variable	: $error (Array)
		@ About		: The array including error code and message on errors
		@ Type		: Public
	*/
	var $error = array();

	/*
		@ Function	: curl_vbulletin() - Constructor
		@ About		: Check if CURL is available and the url exists.
		@ Type		: Public
	*/
	function curl_vbulletin($vbulletin_url, $cookie_name = 'tmpfile.tmp')
	{
		// Check CURL is present
		if ( ! function_exists ( 'curl_init') )
		{
			// Output an error message
			trigger_error('curl_vbulletin::error, Sorry but it appears that CURL is not loaded, Please install it to continue.');
			return false;
		}
		if ( empty($vbulletin_url) )
		{
			// Output an error message
			trigger_error('curl_vbulletin::error, The vbulletin location is required to continue, Please edit your script.');
			return false;
		}
		// Set base location
		$this->vbulletin_url = $vbulletin_url;
		// Create temp file
		$this->cookie_name = $cookie_name;
	}

	/*
		@ Function	: login() - Log In
		@ About		: Does a remote login to the target vbulletin and stores in cookie
		@ Type		: Public
	*/
	function login($username, $password, $ext)
	{
		global $_SERVER;
	
		// Generate post string
		$post_fields = $this->array_to_http(array(
			'vb_login_username'	=> $username,
			'vb_login_password'	=> $password,
			'cookieuser'	=> 1,
			's'	=> '',
			'securitytoken'		=> 'guest',
			'do'		=> 'login'
		));
		// Init curl
		$this->curl = curl_init();
		// Set options
		curl_setopt ( $this->curl, CURLOPT_URL, $this->vbulletin_url . $ext );
		curl_setopt ( $this->curl, CURLOPT_POST, true );
		curl_setopt ( $this->curl, CURLOPT_POSTFIELDS, $post_fields );
		curl_setopt ( $this->curl, CURLOPT_RETURNTRANSFER, true );
		curl_setopt ( $this->curl, CURLOPT_HEADER, false );
		curl_setopt ( $this->curl, CURLOPT_COOKIE, $this->cookie_name );
		curl_setopt ( $this->curl, CURLOPT_COOKIEJAR, $this->cookie_name );
		curl_setopt ( $this->curl, CURLOPT_COOKIEFILE, $this->cookie_name );
		curl_setopt ( $this->curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT'] );
		curl_setopt ( $this->curl, CURLOPT_REFERER, "http://$vbulletin_url/"); 
		// Execute request
		$result = curl_exec ( $this->curl );
		// Error handling
		if ( curl_errno ( $this->curl ) )
		{
			$this->error = array(
				curl_errno($this->curl),
				curl_error($this->curl),
			);
			curl_close ( $this->curl );
			return false;
		}
		// Close connection
		curl_close ( $this->curl );
		// Return result
		return $result;
	}

	/*
		@ Function	: read() - Read a pages contents
		@ About		: Returns the contents of a url
		@ Type		: Public
	*/
	function read($page_url)
	{
		global $_SERVER;

		// Init curl
		$this->curl = curl_init();
		// Set options
		curl_setopt ( $this->curl, CURLOPT_URL, $this->vbulletin_url . $page_url );
		curl_setopt ( $this->curl, CURLOPT_POST, false );
		curl_setopt ( $this->curl, CURLOPT_RETURNTRANSFER, true );
		curl_setopt ( $this->curl, CURLOPT_HEADER, false );
		curl_setopt ( $this->curl, CURLOPT_COOKIE, $this->cookie_name );
		curl_setopt ( $this->curl, CURLOPT_COOKIEJAR, $this->cookie_name );
		curl_setopt ( $this->curl, CURLOPT_COOKIEFILE, $this->cookie_name );
		curl_setopt ( $this->curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT'] );
		// Execute request
		$result = curl_exec ( $this->curl );
		// Error handling
		if ( curl_errno ( $this->curl ) )
		{
			$this->error = array(
				curl_errno($this->curl),
				curl_error($this->curl),
			);
			curl_close ( $this->curl );
			return false;
		}
		// Close connection
		curl_close ( $this->curl );
		// Return result
		return $result;
	}

	/*
		@ Function	: logout() - Log Out
		@ About		: Logs out of the target vbulletin properly.
		@ Type		: Public
	*/
	function logout()
	{
		global $_SERVER;
	
		// Generate post string
		$urlopt = $this->array_to_http(array(
			'logout'	=> 'true',
			'mode'		=> 'logout',
		));
		// Init curl
		$this->curl = curl_init();
		// Set options
		curl_setopt ( $this->curl, CURLOPT_URL, $this->vbulletin_url . 'login.php?do=logout' );
		curl_setopt ( $this->curl, CURLOPT_POST, false );
		curl_setopt ( $this->curl, CURLOPT_RETURNTRANSFER, true );
		curl_setopt ( $this->curl, CURLOPT_HEADER, false );
		curl_setopt ( $this->curl, CURLOPT_COOKIE, $this->cookie_name );
		curl_setopt ( $this->curl, CURLOPT_COOKIEJAR, $this->cookie_name );
		curl_setopt ( $this->curl, CURLOPT_COOKIEFILE, $this->cookie_name );
		curl_setopt ( $this->curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT'] );
		// Execute request
		$result = curl_exec ( $this->curl );
		// Error handling
		if ( curl_errno ( $this->curl ) )
		{
			$this->error = array(
				curl_errno($this->curl),
				curl_error($this->curl),
			);
			curl_close ( $this->curl );
			return false;
		}
		// Close connection
		curl_close ( $this->curl );
		// Delete cookie file
		@unlink($this->cookie_name);
		// Return result
		return true;
	}
	
	/*
		@ Function	: array_to_http() - Converter
		@ About		: Converts data from array to http string
		@ Type		: Private
	*/
	function array_to_http($array)
	{
		$retvar = '';
		while ( list ( $field, $data ) = @each ( $array ) )
		{
			$retvar .= ( empty($retvar) ) ? '' : '&';
			$retvar .= urlencode($field) . '=' . urlencode($data); 
		}
		return $retvar;
	}
}

?>